#include <stdio.h>
#include<iostream>
using namespace std;
class Solution {
public:
    int lastRemaining(const int n, int m)
    {
        int arr[5];
        memset(arr, 1, sizeof(arr));
        int num = n, i = 1;
        while (num != 1)
        {
            if (i% m == 0 && arr[(i - 1) % n] != 0)
            {
                num--;
                arr[(i - 1)%n] = 0;
                i++;
            }
            else if(i % m == 0 && arr[(i - 1) % n] == 0)
            {
                i++;
                continue;
            }
            else
            {
                i++;
            }
        }
        for (int x = 0; x < n; x++)
        {
            if (arr[x] == 1)
                return arr[x];
        }
        return 0;
    }
};


int main()
{
    Solution mysolution;
    int i=mysolution.lastRemaining(5, 3);
    cout << i;
    return 0;
}